package com.baizhiedu.factorybean;

import org.springframework.beans.factory.FactoryBean;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * ClassName: ConnectionFactoryBean
 * Description:
 * Date: 2022/11/19 PM 4:17:18
 * <p>
 * project: spring5
 * package: com.baizhiedufactorybean
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class ConnectionFactoryBean implements FactoryBean<Connection> {

    private String driverClassName;
    private String url;
    private String username;
    private String password;

    @Override
    public Connection getObject() throws Exception {
        Class.forName(driverClassName);
        return DriverManager.getConnection(url, username, password);
    }

    @Override
    public Class<?> getObjectType() {
        return Connection.class;
    }

    /**
     * 返回 true 只会创建一个对象
     * 返回 false 每次都会创建新的对象
     * 问题：根据这个对象的特点，决定是返回 true (SqlSessionFactory) 还是 false (Connection)
     *
     * @return true or false
     */
    @Override
    public boolean isSingleton() {
        return false;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
